﻿/*************************************************************
 * Software of blackcore
 *
 * CLRVersion:4.0.30319.586
 * MachineName:SPACE
 * Author:Andy Huang
 * Email:itblackhole@gmail.com
 * DateTime：6/20/2013 11:21:37 AM
 * Function：
 *************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web;
using System.Web.UI;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Data;

namespace SobFS.Common.UserControl
{
    [DefaultProperty("Text"), ToolboxData("<{0}:DropDownTree runat=\"server\"></{0}:DropDownTree>")]
    public class DropDownTree : DropDownList, INamingContainer
    {
        #region ******************************Private Property******************************
        private string nodeFlag = "├";
        private int nodeId;
        #endregion ******************************Private Property******************************


        #region ******************************Public Property******************************

        public int NodeId
        {
            get
            {
                nodeId = Convert.ToInt32(this.SelectedValue.Trim());
                return nodeId;
            }
            set
            {
                nodeId = value;
            }
        }
        #endregion ******************************Public Property******************************


        #region ******************************Structure******************************
        /// <summary>
        /// 
        /// </summary>
        /// <param name="source">id,name</param>
        public DropDownTree()
        { }
        #endregion  ******************************Structure******************************


        #region ******************************Private Method******************************
        private void BuildTreeView(DataTable dataTable, string nodeId)
        {
            DataRow[] drs = dataTable.Select("ParentId = " + nodeId, "Position");
            foreach (DataRow dr in drs)
            {
                if (dr == null) continue;
                if (dr["Id"] == null || dr["ParentId"] == null) continue;

                string id = Convert.ToString(dr["Id"]);
                var li = new ListItem(string.Format("{0} {1}", nodeFlag, dr["Name"].ToString()), id);
                this.Items.Add(li);
                nodeFlag += "─┴";

                BuildTreeView(dataTable, id);
                nodeFlag = nodeFlag.Substring(0, nodeFlag.Length - 2);
            }
        }
        #endregion ******************************Private Method******************************


        #region ******************************Public Method******************************
        public void Bind(DataSet source)
        {
            if (source != null && source.Tables != null && source.Tables.Count > 0)
            {
                Bind(source.Tables[0]);
            }
            else
            {
                this.Items.Clear();
                this.Items.Add(new ListItem("请选择...", "-1"));
            }

            this.SelectedIndex = -1;
        }


        public void Bind(DataTable source)
        {
            this.Items.Clear();
            this.Items.Add(new ListItem("请选择...", "0"));

            if (source != null && source.Rows != null && source.Rows.Count > 0)
            {
                DataRow[] drs = source.Select("ParentId IS NULL OR ParentId = 0", "Position");
                foreach (DataRow dr in drs)
                {
                    if (dr == null) continue;
                    if (dr["Id"] == null || dr["ParentId"] == null) continue;

                    string id = Convert.ToString(dr["Id"]);
                    var li = new ListItem(string.Format("{0} {1}", nodeFlag, dr["Name"].ToString()), id);
                    this.Items.Add(li);
                    nodeFlag += "─┴";

                    BuildTreeView(source, id);
                    nodeFlag = nodeFlag.Substring(0, nodeFlag.Length - 2);
                }
            }
        }
        #endregion ******************************Public Method******************************
    }
}








